package cn.smbms.controller;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.service.BillService;
import cn.smbms.service.ProviderService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/pro")
public class ProviderController {

    @Resource
    private ProviderService providerService;

    @Resource
    private BillService billService;

    @RequestMapping(value = "/pro.html")
    public String toPro(Model model,
                        @RequestParam(value = "queryProCode",required = false) String proCode,
                        @RequestParam(value = "queryProName",required = false) String proName,
                        @RequestParam (value = "pageIndex", required = false,defaultValue = "1") Integer PageNo,
                        @RequestParam(value = "pageSize", required = false,defaultValue = "5") Integer pageSize){
        //总数量（表）
        Integer totalCount = providerService.getProviderCount(proCode,proName);

        int totalPageCount = totalCount/pageSize;
        if(totalCount%pageSize!=0){
            totalPageCount++;
        }

        //控制首页和尾页
        if(PageNo < 1){
            PageNo = 1;
        }else if(PageNo > totalPageCount){
            PageNo = totalPageCount;
        }



        List<Provider> providerList = providerService.getProListlimit(proCode,proName,PageNo,pageSize);
        model.addAttribute("providerList",providerList);

        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", PageNo);
        return "providerlist";
    }

    @RequestMapping(value = "toAdd")
    public String toAdd(){
        return "provideradd";
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "proid") Integer id){
        boolean check = providerService.delete(id);
        Integer count = billService.count(id);
        HashMap<String,String> data = new HashMap<String,String>();
        if(count==null||count==0){
            if(id!=null){
                if(check){
                    data.put("delResult","true");
                }else{
                    data.put("delResult","false");
                }
            }else{
                data.put("delResult","notexist");
            }
        }else{
            data.put("delResult",count.toString());
        }

        return JSONArray.toJSONString(data);
    }


    @RequestMapping(value = "/view/{id}")
    public String viewPro(Model model,
                           @PathVariable(value = "id") Integer id){
        Provider provider = providerService.getById(id);
        model.addAttribute("provider",provider);
        return "providerview";

    }

    @RequestMapping(value = "/toModify/{id}")
    public String toModify(Model model,
                          @PathVariable(value = "id") Integer id){
        Provider provider = providerService.getById(id);
        model.addAttribute("provider",provider);
        return "providermodify";

    }

    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public String modifyUser(Provider provider,Model model){
        boolean check = providerService.modify(provider);
        if(!check){
            return "providermodify";
        }
        return "redirect:/pro/pro.html";
    }

    @RequestMapping(value = "/add.html")
    public String add(Provider provider){
        provider.setCreationDate(new Date());
        boolean check = providerService.add(provider);
        if(check){
            return "redirect:/pro/pro.html";
        }
        return "proadd";
    }

}
